package com.example.framework.activity;

import com.example.framework.R;
import com.example.framework.base.BaseActivity;
import com.example.framework.base.CustomApplication;
import com.example.framework.interfaces.AlertDialogBtnClickListenerInterface.OnPositiveButtonClickListener;
import com.example.framework.interfaces.HandlerInterface;
import com.example.framework.interfaces.AsyncTaskInterface.*;
import com.example.framework.model.ResModel;
import com.example.framework.specialeffects.PathButtonActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends BaseActivity implements OnClickListener, HandlerInterface
{
	String string;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.i("lala", "MainActivity:----->onCreate");
		findViewById(R.id.btn1).setOnClickListener(this);
		findViewById(R.id.btn2).setOnClickListener(this);
		findViewById(R.id.btn3).setOnClickListener(this);
		findViewById(R.id.btn4).setOnClickListener(this);
		findViewById(R.id.btn5).setOnClickListener(this);
		findViewById(R.id.btn6).setOnClickListener(this);
		findViewById(R.id.btn7).setOnClickListener(this);
		findViewById(R.id.btn8).setOnClickListener(this);
	}

	@Override
	protected void onStart()
	{
		super.onStart();
		Log.i("lala", "MainActivity:----->onStart");
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		Log.i("lala", "MainActivity:----->onResume");
	}
	
	@Override
	protected void onRestart()
	{
		super.onRestart();
		Log.i("lala", "MainActivity:----->onRestart");
	}
	
	@Override
	protected void onPause()
	{
		super.onPause();
		Log.i("lala", "MainActivity:----->onPause");
	}
	
	@Override
	protected void onStop()
	{
		super.onStop();
		Log.i("lala", "MainActivity:----->onStop");
	}
	
	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		Log.i("lala", "MainActivity:----->onDestroy");
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			long secondTime = System.currentTimeMillis();
			if (secondTime - firstTime > 2000)
			{
				Toast.makeText(this, R.string.againtoexit, Toast.LENGTH_SHORT).show();
				firstTime = secondTime;
				return true;
			}
			else
			{
				CustomApplication.getInstance().exit(0);
			}
		}
		return super.onKeyUp(keyCode, event);
	}
	
	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.btn1:
				showAlertDialog("you can set null to hide title", "this is message","okButton",new OnPositiveButtonClickListener()
				{
					@Override
					public void OnPositiveButtonClick()
					{
						
					}
				});
				break;
			case R.id.btn2:
				showProgressDialog();
				break;
			case R.id.btn3:
				doPostAsync(new OnPreCall()
				{
					@Override
					public void SetUrlAndParams(PreCallListener preCallListener)
					{
						preCallListener.SetUrl("http://192.168.0.79:8080/LianHePayServer/app");
						preCallListener.SetParams("{\"msg\":\"2001\",\"phone\":\"13791842332\",\"iInStoreId\":\"140\"}");
					}
				}, new OnCallBack()
				{
					@Override
					public void OnSuccess(String callbackValue)
					{
						ResModel model = gson.fromJson(callbackValue, ResModel.class);
						showToast(model.getCode() + "----" + model.getMsg());
					}
					
					@Override
					public void OnFailed()
					{
						
					}
					
					@Override
					public void OnError()
					{
						
					}
				});
				break;
			case R.id.btn4:
				myHandler = new MyHandler(this,new HandlerInterface()
				{
					@Override
					public void handleMessage(Message msg)
					{
						showToast("myhandler");
					}
				});
				myHandler.obtainMessage().sendToTarget();
				break;
			case R.id.btn5:
				ResModel model = gson.fromJson("{\"code\":\"2001\",\"msg\":\"ok\"}", ResModel.class);
				showToast(model.getCode() + "----" + model.getMsg());
				
				/* another method.
				try
				{
					JSONObject jsonObject = new JSONObject("{\"code\":\"2001\",\"msg\":\"ok\"}");
					String code = jsonObject.getString("code");
				}
				catch (JSONException e)
				{
					e.printStackTrace();
				}*/
				break;
			case R.id.btn6:
				string.toUpperCase();
				break;
			case R.id.btn7:
				Intent intent = new Intent(this, Activity1.class);
				startActivity(intent);
				break;
			case R.id.btn8:
				Intent in = new Intent(this, PathButtonActivity.class);
				startActivity(in);
				break;
			default:
				break;
		}
	}

	@Override
	public void handleMessage(Message msg)
	{
		showToast(msg.obj.toString());
	}

}
